package com.exercise.memberscheduling.domain.entity;

import com.exercise.memberscheduling.domain.valueObject.Member;
import com.exercise.memberscheduling.domain.valueObject.MemberOperate;
import com.exercise.memberscheduling.repository.service.MemberService;
import com.exercise.memberscheduling.tools.getCurrentTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MemberEntity {
    @Autowired
    private MemberService memberService;
    private Member member = new Member();
    private MemberOperate memberOperate = new MemberOperate();
    private OperateType operateType ;
    //会员购买课程 参数（会员id，购买课程数，未购买前剩余的课程数）
    public void BuyCourseTimes(int memberId,int times)
    {
        member.setMemberId(memberId);
        memberOperate.setMemberId(memberId);
        operateType= OperateType.BuyCourseTimes;
        memberOperate.setOperateType(operateType.getOperate());
        memberOperate.setTimesChange(times);
        memberOperate.setCurrentTime(getCurrentTime.getTime());
        //用未购买课程前的课程次数加上本次购买的次数，即购买后的次数
        int BeforeRemainTimes = selMemberById(member).getRemainTimes();
        memberOperate.setLeftTimes(BeforeRemainTimes+times);
        memberService.addCoursesTimes(times,member,memberOperate);
    }
    //会员来上课()
    public boolean BeginClass(int memberId)
    {
        member.setMemberId(memberId);
        memberOperate.setMemberId(memberId);
        operateType= OperateType.ClassBegin;
        memberOperate.setOperateType(operateType.getOperate());
        memberOperate.setCurrentTime(getCurrentTime.getTime());
        memberOperate.setTimesChange(1);
        int leftTimes = memberService.selectMemberLeftCourses(member).getRemainTimes()-1;
        if(leftTimes<0)
        {
            return false;
        }
        memberOperate.setLeftTimes(leftTimes);
        memberService.BeginClass(member,memberOperate);
        return true;
    }
    //添加一个会员
    public void addMember(Member member)
    {
        memberService.addMemberInfo(member);
    }
    //查询所有会员信息
    public List<Member> selAllMembers()
    {
        return memberService.selAllMembers();
    }
    //根据id查询会员信息
    public Member selMemberById(Member member)
    {
        return memberService.selectMemberLeftCourses(member);
    }
    //根据姓名查询会员信息
    public List<Member> selMemberByName(String memberName)
    {
        String s = "";
        for(int i = 0;i<memberName.length();i++)
        {
            s+="%";
            s+=memberName.charAt(i);
        }
        s+="%";
        member.setMemberName(s);
        return memberService.selectMemberByName(member);
    }
    //查询所有操作记录
    public List<MemberOperate> selAllOperate()
    {
        return memberService.selAllOperate();
    }
    //根据用户名查找操作记录
    public List<MemberOperate>selOperateByMemberName(Member member)
    {
        return memberService.selOperateByMemberName(member);
    }
    //更改会员姓名
    public void changeMemberName(int memberId,String memberName)
    {
        member.setMemberId(memberId);
        member.setMemberName(memberName);
        memberService.changeMemberName(member);
    }
    //更改用户课程数
    public void changeCoursesTimes(int memberId,int changedTimes)
    {
        member.setMemberId(memberId);
        member.setRemainTimes(changedTimes);
        memberOperate.setMemberId(memberId);
        int leftTimes = memberService.selectMemberLeftCourses(member).getRemainTimes()-changedTimes;
        if(leftTimes<0){
            leftTimes*=-1;
            operateType = OperateType.reviseAdd;
        }else
        {
            operateType = OperateType.reviseReduce;
        }
        memberOperate.setOperateType(operateType.getOperate());
        memberOperate.setTimesChange(leftTimes);
        memberOperate.setCurrentTime(getCurrentTime.getTime());
        memberOperate.setLeftTimes(changedTimes);
        memberService.reviseMemberCoursesTimes(member,memberOperate);
    }
    //更改管理员密码
    public void reviseAdminPassword(String newPassword)
    {
        memberService.reviseAdminPassword(newPassword);
    }
    //删除一个会员
    public void delMember(String memberId)
    {
        memberService.deleteMember(memberId);
    }
    //查询一段时间内的上课数
    public List<Object> periodBeginsCourses(String startTime,String endTime)
    {
        int BeginCourseSums = 0;
        int BuyCourseSums = 0;
        startTime.replace("/","-");
        endTime.replace("/","-");
        endTime+=":24:00:00";
        List<MemberOperate> memberOperates = memberService.selPeriodOperate(startTime,endTime);
        for(MemberOperate o:memberOperates)
        {
            if(o.getOperateType().equals(OperateType.ClassBegin.getOperate()))
            {
                BeginCourseSums++;
            }
            if(o.getOperateType().equals(OperateType.BuyCourseTimes.getOperate()))
            {
                BuyCourseSums+=o.getTimesChange();
            }
        }
        List<Object> sums = new ArrayList<>();
        sums.add(BeginCourseSums);
        sums.add(BuyCourseSums);
        sums.add(memberOperates);
        return sums;
    }
}
